package com.dms.modules.community.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dms.common.api.ApiResponse;
import com.dms.modules.community.dto.CommunityFollowDTO;
import com.dms.modules.community.service.CommunityFollowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.dms.common.security.SecurityUtils;

@Slf4j
@Api(tags = "社区关注接口")
@RestController
@RequestMapping("/community/follow")
public class CommunityFollowController {

    @Autowired
    private CommunityFollowService followService;

    @PostMapping("/{followedUserId}")
    @ApiOperation("关注用户")
    public ApiResponse<Boolean> followUser(
            @PathVariable Long followedUserId) {
        Long userId = SecurityUtils.getCurrentUserId();
        return ApiResponse.success(followService.followUser(userId, followedUserId));
    }

    @DeleteMapping("/{followedUserId}")
    @ApiOperation("取消关注用户")
    public ApiResponse<Boolean> unfollowUser(
            @PathVariable Long followedUserId) {
        Long userId = SecurityUtils.getCurrentUserId();
        return ApiResponse.success(followService.unfollowUser(userId, followedUserId));
    }

    @GetMapping("/following")
    @ApiOperation("获取关注列表")
    public ApiResponse<IPage<CommunityFollowDTO>> getFollowingList(
            @ApiParam(value = "页码", defaultValue = "1")
            @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页数量", defaultValue = "10")
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Long userId = SecurityUtils.getCurrentUserId();
        Page<CommunityFollowDTO> page = new Page<>(pageNum, pageSize);
        return ApiResponse.success(followService.getFollowingList(userId, page));
    }

    @GetMapping("/followers")
    @ApiOperation("获取粉丝列表")
    public ApiResponse<IPage<CommunityFollowDTO>> getFollowerList(
            @ApiParam(value = "页码", defaultValue = "1")
            @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页数量", defaultValue = "10")
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Long userId = SecurityUtils.getCurrentUserId();
        Page<CommunityFollowDTO> page = new Page<>(pageNum, pageSize);
        return ApiResponse.success(followService.getFollowerList(userId, page));
    }

    @GetMapping("/check/{followedUserId}")
    @ApiOperation("检查是否已关注")
    public ApiResponse<Boolean> checkIsFollowed(
            @PathVariable Long followedUserId) {
        Long userId = SecurityUtils.getCurrentUserId();
        return ApiResponse.success(followService.checkIsFollowed(userId, followedUserId));
    }
} 